package com.diablo.sitemanager.controller;

import com.diablo.sitemanager.entity.Article;
import com.diablo.sitemanager.entity.User;
import com.diablo.sitemanager.service.ArticleService;
import com.diablo.sitemanager.util.FileUtil;
import com.diablo.sitemanager.util.QiniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Random;

/**
 * Created by diablo13064 on 2018/6/5.
 */
@Controller
@RequestMapping("/article")
public class ArticleController {
    private static String dir = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"/static/images/";
    @Autowired
    QiniuUtil qiniuUtil;
    @Autowired
    ArticleService articleService;
    @RequestMapping("/submit")
    public String submitArticle(ModelMap modelMap, HttpSession session, MultipartFile file, Article article, String createtime) throws ParseException, IOException {

        if (createtime != null) {
            article.setCreattime(FileUtil.String2Date(createtime, "yyyy-MM-dd"));
        }
        User user = (User) session.getAttribute("loginUser");
        if (user != null) {
            article.setAuthorid(user.getUid());
        } else {
            article.setAuthorid(1);
        }
        if (file != null && file.getOriginalFilename().indexOf(".") > -1) {             //上传图片
            String fileName = FileUtil.makeFileName(file);
            qiniuUtil.upload(file.getBytes(), fileName);
            article.setImglink(qiniuUtil.getPublicDownLink(fileName));
        } else {                                                                        //设置本地图片
            String dir = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"static" + File.separator + "images" + File.separator;
            File[] files = new File(dir).listFiles();
            int randomCount = new Random(System.currentTimeMillis()).nextInt(files.length);
            while (true) {
                if (files[randomCount].getName().endsWith(".jpg")) {
                    article.setImglink(dir+ File.separator+"images"+File.separator+files[randomCount].getName());
                    break;
                }
            }
        }
        //更新文章
        if (article.getAid() != null) {

            return "";
        }
        articleService.addArticle(article);
        return "redirect:/";
    }


    @RequestMapping("/delete")
    @ResponseBody
    public String deleteArticle(String aid) {
        Long articleId = Long.parseLong(aid);
        int statusCode = articleService.deleteArticle(articleId);
        if (statusCode > 0) {
            return "success";
        }
        return "failure";
    }
}
